package com.jh.storageview;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

import org.w3c.dom.Text;

import java.lang.reflect.Array;
import java.util.Arrays;

public class StoragePreference extends Preference {

    public static final String TAG = "StoragePreference";

    Context mContext;
    int[] dys = new int[8];
    TextView mTitle;
    TextView mSummary;
    View mMask;
    View smallIcon;
    View rootView;
    ImageView imageView;

    public StoragePreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        setLayoutResource(R.layout.storage_preference);
        init();
    }

    public StoragePreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        setLayoutResource(R.layout.storage_preference);
        init();
    }

    public StoragePreference(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setLayoutResource(R.layout.storage_preference);
        init();
    }

    public StoragePreference(@NonNull Context context) {
        super(context);
        mContext = context;
        setLayoutResource(R.layout.storage_preference);
        init();
    }

    private void init(){

    }

    @Override
    public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        rootView = holder.itemView;
        mMask = holder.findViewById(R.id.mask);
        mTitle = (TextView) holder.findViewById(R.id.top_txt);
        mSummary= (TextView) holder.findViewById(R.id.bot_txt);
        smallIcon = holder.findViewById(R.id.left_small_icon);
        imageView = (ImageView) holder.findViewById(R.id.right_icon);
        smallIcon.post(new Runnable() {
            @Override
            public void run() {
                int y = (int) smallIcon.getX();
                int x = (int) smallIcon.getY();
                Log.d(TAG, "onBindViewHolder: x=" + x);
                Log.d(TAG, "onBindViewHolder: y=" + y);

                int rootViewPaddingTop = holder.itemView.getPaddingTop();
                int rootViewPaddingBottom = holder.itemView.getPaddingBottom();
                Log.d(TAG, "run: rootViewPaddingTop = " + rootViewPaddingTop);
                Log.d(TAG, "run: rootViewPaddingBottom = " + rootViewPaddingBottom);

                int dy = rootViewPaddingBottom + rootViewPaddingTop + y;
                Log.d(TAG, "run: dy = "  + dy);

                int height = holder.itemView.getHeight();
                Log.d(TAG, "run: height = " + height);

                String key = getKey();
                if (StorageFragment.STORAGE_SYS_G.equals(key)){
                    dys[0] = dy + height*7;
                    if (dys[0] != 0) DysStatic.integers[0] = dys[0];
                }else if (StorageFragment.STORAGE_SYS_D.equals(key)){
                    dys[1] = dy + height*6;
                    if (dys[1] != 0) DysStatic.integers[1] = dys[1];
                }else if (StorageFragment.STORAGE_DOC.equals(key)){
                    dys[2] = dy + height*5;
                    if (dys[2] != 0) DysStatic.integers[2] = dys[2];
                }else if (StorageFragment.STORAGE_PKG.equals(key)){
                    dys[3] = dy + height*4;
                    if (dys[3] != 0) DysStatic.integers[3] = dys[3];
                }else if (StorageFragment.STORAGE_VIDEO.equals(key)){
                    dys[4] = dy + height*3;
                    if (dys[4] != 0) DysStatic.integers[4] = dys[4];
                }else if (StorageFragment.STORAGE_SOUND.equals(key)){
                    dys[5] = dy + height*2;
                    if (dys[5] != 0) DysStatic.integers[5] = dys[5];
                }else if (StorageFragment.STORAGE_IMG.equals(key)){
                    dys[6] = dy + height*1;
                    if (dys[6] != 0) DysStatic.integers[6] = dys[6];
                }else if (StorageFragment.STORAGE_APP.equals(key)){
                    dys[7] = dy + height*0;
                    if (dys[7] != 0) DysStatic.integers[7] = dys[7];
                }
                if (mStoragePreferenceLayoutLoadFinish != null){
                    mStoragePreferenceLayoutLoadFinish.layoutLoadFinish(DysStatic.integers);
                }
            }
        });
        if (mContext != null){
            if (StorageFragment.STORAGE_SYS_G.equals(getKey())){
                smallIcon.setBackground(mContext.getResources().getDrawable(R.drawable.small_sys_g, null));
                imageView.setVisibility(View.INVISIBLE);
            } else if (StorageFragment.STORAGE_SYS_D.equals(getKey())) {
                smallIcon.setBackground(mContext.getResources().getDrawable(R.drawable.small_sys_d, null));
            }else if (StorageFragment.STORAGE_DOC.equals(getKey())) {
                smallIcon.setBackground(mContext.getResources().getDrawable(R.drawable.small_doc, null));
            }else if (StorageFragment.STORAGE_PKG.equals(getKey())) {
                smallIcon.setBackground(mContext.getResources().getDrawable(R.drawable.small_pkg, null));
            }else if (StorageFragment.STORAGE_VIDEO.equals(getKey())) {
                smallIcon.setBackground(mContext.getResources().getDrawable(R.drawable.small_video, null));
            }else if (StorageFragment.STORAGE_SOUND.equals(getKey())) {
                smallIcon.setBackground(mContext.getResources().getDrawable(R.drawable.small_sound, null));
            }else if (StorageFragment.STORAGE_IMG.equals(getKey())) {
                smallIcon.setBackground(mContext.getResources().getDrawable(R.drawable.small_img, null));
            }else if (StorageFragment.STORAGE_APP.equals(getKey())) {
                smallIcon.setBackground(mContext.getResources().getDrawable(R.drawable.small_app, null));
            }
        }
    }

    public void setBackgroundMask(){
        if (rootView != null){
            rootView.setAlpha(0.2f);
        }
    }
    public void removeBackgroundMask(){
        if (rootView != null){
            rootView.setAlpha(1f);
        }
    }

    public void setSTitle(String title){
        mTitle.post(new Runnable() {
            @Override
            public void run() {
                mTitle.setText(title);
            }
        });
    }

    public void setSSummary(String summary){
        mSummary.post(new Runnable() {
            @Override
            public void run() {
                mSummary.setText(summary);
            }
        });

    }

    private StoragePreferenceLayoutLoadFinish mStoragePreferenceLayoutLoadFinish;
    public interface StoragePreferenceLayoutLoadFinish{
        void layoutLoadFinish(int[] dy);
    }
    public void setStoragePreferenceLayoutLoadFinish(StoragePreferenceLayoutLoadFinish listener){
        mStoragePreferenceLayoutLoadFinish = listener;
    }
}
